const electron = require('electron');
const { app } = electron;
const { BrowserWindow } = electron;
let  udxdata_win;
let SureToExit = false;

/**********************************Dialog*********** */

const main_ipc = electron.ipcMain;
const dialog = electron.dialog;

//save to folder
main_ipc.on('saveToFolder-dialog', function (event) {
  const options = {
    title: 'Choose Folder To Save',
    properties: ['openFile', 'openDirectory']
  }
  dialog.showOpenDialog(options, function (filename) {
    event.sender.send('saveToFolder-path', filename);
  })
})

//save UDX file dialog
main_ipc.on('saveUDXFile-dialog', function (event) {
  const options = {
    title: 'Save UDX File',
    filters: [
      { name: 'Files', extensions: ['xml'] }
    ]
  }
  dialog.showSaveDialog(options, function (filename) {
    event.sender.send('saveUDXFile-path', filename);
  })
})

//save format file dialog
main_ipc.on('saveFormatFile-dialog', function (event) {
  const options = {
    title: 'Save Format File',
    filters: [
      { name: 'Files', extensions: ['*'] }
    ]
  }
  dialog.showSaveDialog(options, function (filename) {
    event.sender.send('saveFormatFile-path', filename);
  })
})

//save schema file 
main_ipc.on('saveUdxSchemaFile-dialog', function (event) {
  const options = {
    title: 'Save UDX Schema File',
    filters: [
      { name: 'Files', extensions: ['xml'] }
    ]
  }
  dialog.showSaveDialog(options, function (filename) {
    event.sender.send('saveUdxSchemaFile-path', filename);
  })
})

//choose UDX file Dialog
main_ipc.on('openUDXFile-dialog', function (event) {
  const options = {
    title: 'Choose UDX File',
    filters: [
      { name: 'Files', extensions: ['xml', 'zip'] }
    ]
  }
  dialog.showOpenDialog(options, function (filename) {
    event.sender.send('openUDXFile-path', filename);
  })
})

//choose format file dialog
main_ipc.on('openFormatFile-dialog', function (event) {
  const options = {
    title: 'Choose Format File',
    filters: [
      { name: 'Files', extensions: ['*'] }
    ]
  }
  dialog.showOpenDialog(options, function (filename) {
    event.sender.send('openFormatFile-path', filename);
  })
})

//choose folder dialog
main_ipc.on('openFolder-dialog', function (event) {
  const options = {
    title: 'Choose Mapping Method Folder',
    properties: ['openFile', 'openDirectory']
  }
  dialog.showOpenDialog(options, function (filename) {
    event.sender.send('openFolder-path', filename);
  })
})

//load udx shcema
main_ipc.on('openUdxSchemaFile-dialog', function (event) {
  const options = {
    title: 'Choose File',
    filters: [
      { name: 'Files', extensions: ['xml'] }
    ]
  }
  dialog.showOpenDialog(options, function (filename) {
    event.sender.send('openUdxSchemaFile-path', filename);
  })
})

//close window
main_ipc.on('close-udx-window', function (event) {
  SureToExit = true;
  if (udxdata_win !== undefined) {
    udxdata_win.close();
  }
})

/**data_prepare dialog*/

//load udx shcema
//save schema file 
main_ipc.on('saveZipUdx-dialog', function (event) {
  const options = {
    title: 'Save UDX by zip format',
    filters: [
      { name: 'Files', extensions: ['zip'] }
    ]
  }
  dialog.showSaveDialog(options, function (filename) {
    event.sender.send('saveZipUdx-path', filename);
  })
})

/**********************************Dialog*********** */

/*******************start udx data window*/

function createUdxDataWindow() {
  // Create the browser window.
  udxdata_win = new BrowserWindow({
    width: 1100,
    height: 710,
    minWidth: 1100,
    minHeight: 710, backgroundColor: '#ffffff'
  });

  //maximize
  //udxdata_win.maximize();

  // and load the index.html of the app.
  udxdata_win.loadURL(`file://${__dirname}/udx-data.html`);

  // Open the DevTools.
  //win.webContents.openDevTools();

  udxdata_win.on('close', (event) => {
    if (!SureToExit) {
      event.sender.send('window-close');
      event.preventDefault();
    }

  })

  // Emitted when the window is closed.
  udxdata_win.on('closed', () => {
    udxdata_win = null;
  });
}
/*******************end udx data window */

app.on('ready', createUdxDataWindow);

app.on('window-all-closed', () => {

  if (process.platform !== 'darwin') {
    app.quit();
  }
});
